package net.starwifi.duobao.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.ArrayList;
import java.util.List;

/**
 * 夺宝商品
 * Created by ruoo on 2015/11/19.
 */
@Entity
@Table(name = "duob_db_product")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "duob_db_product_sequence")
public class DBProduct extends OrderEntity {

    public final static Integer FLAG_DEFAULT=0;

    public final static Integer FLAG_YX=1;

    /** 编号 */
    private String sn;

    /** 名称 */
    private String name;

    /** 全称 */
    private String fullName;

    /** 是否上架(上架状态不允许下架，下架状态上架会发布新的夺宝期数) */
    private Boolean isMarketable;

    /** 是否自动商家（当期结束之后自动开始下期）*/
    private Boolean isAutoMarketable;

    /** 介绍 */
    private String introduction;

    /** 备注 */
    private String memo;

    /** 夺宝商品图片 */
    private List<DBProductImage> productImages = new ArrayList<DBProductImage>();

    /** 最新一期的夺宝 */
    private DBTimes dbTimes;

    /** 特殊标志（暂时给亚信的产品使用）*/
   // private Integer flag;

    private DBProductType dbProductType;


    @JsonProperty
    @Pattern(regexp = "^[0-9a-zA-Z_-]+$")
    @Length(max = 100)
    @Column(nullable = false, unique = true, length = 100)
    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    /**
     * 获取名称
     *
     * @return 名称
     */
    @JsonProperty
    @NotEmpty
    @Length(max = 200)
    @Column(nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    @JoinColumn(name = "db_product_type",updatable = false)
    public DBProductType getDbProductType() {
        return dbProductType;
    }

    public void setDbProductType(DBProductType dbProductType) {
        this.dbProductType = dbProductType;
    }

    /**
     * 获取全称
     *
     * @return 全称
     */
    @JsonProperty
    @Column(nullable = false)
    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }



    @Column
    public Boolean getIsMarketable() {
        return isMarketable;
    }

    public void setIsMarketable(Boolean isMarketable) {
        this.isMarketable = isMarketable;
    }

    @Column
    public Boolean getIsAutoMarketable() {
        return isAutoMarketable;
    }

    public void setIsAutoMarketable(Boolean isAutoMarketable) {
        this.isAutoMarketable = isAutoMarketable;
    }

    /**
     * 获取介绍
     *
     * @return 介绍
     */
    @Lob
    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    /**
     * 获取备注
     *
     * @return 备注
     */
    @Length(max = 200)
    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    /**
     * 获取商品图片
     *
     * @return 商品图片
     */
    @Valid
    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "duob_db_product_image")
    public List<DBProductImage> getProductImages() {
        return productImages;
    }

    public void setProductImages(List<DBProductImage> productImages) {
        this.productImages = productImages;
    }

    @OneToOne
    @JoinColumn(name = "db_times_id")
    public DBTimes getDbTimes() {
        return dbTimes;
    }

    public void setDbTimes(DBTimes dbTimes) {
        this.dbTimes = dbTimes;
    }

//    @Column
//    public Integer getFlag() {
//        return flag;
//    }
//
//    public void setFlag(Integer flag) {
//        this.flag = flag;
//    }

    /**
     * 获取缩略图
     *
     * @return 缩略图
     */
    @JsonProperty
    @Transient
    public String getLarge() {
        if (getProductImages() != null && !getProductImages().isEmpty()) {
            return getProductImages().get(0).getLarge();
        }
        return null;
    }


    /**
     * 获取缩略图
     *
     * @return 缩略图
     */
    @JsonProperty
    @Transient
    public String getThumbnail() {
        if (getProductImages() != null && !getProductImages().isEmpty()) {
            return getProductImages().get(0).getThumbnail();
        }
        return null;
    }


    @JsonProperty
    @Transient
    public String getMedium() {
        if (getProductImages() != null && !getProductImages().isEmpty()) {
            return getProductImages().get(0).getMedium();
        }
        return null;
    }

    /** 获取进度 */
    @JsonProperty
    @Transient
    public Integer getProgress(){
        Integer current=dbTimes.getCurrentQuantity();
        Integer sum=dbTimes.getQuantity();
        return (current/sum)*100;
    }

}
